function M = extraTarget2cell(str_cell,CommonPart)
%extraTarget2cell - Generation of cell variable from total station data
%
%   C = extraTarget2cell(strCell,CommonPart)
%
% This function takes the position data related to some targets of a 
% total station, where strCell is a cell variable of strings whith the 
% target names, and CommonPart is a string of the common part of the 
% input filenames. The data related to the k-th target are taken from the
% file whose name is [CommonPart strCell(k) '.xlsx'].
% The output cell C is such that C{k,1} is strCell(k), i.e. the name of  
% the k-th target, and C{k,2} is the data matrix, according to the input
% file.
% If strCell is undefined or empty, this cell is used:
%   strCell = {'P1','P2','P3','P4','P5','P6','P7','P8','P9','P10','P11',...
%     'P12','P13','P14','P15','P16','P17','P18','P19','P20','P21','P22',...
%     'P23','P24','P25','P26','P27','P28','P29','P30','P31',...
%     'N1','N2','N3','N4','N5','N6','N7','N8','N9','N10',...
%     'GPS_ROV 1','GPS_ROV 2',...
%     'R1','R1_BIS','R2','R2BIS','R3','R4','R5'}.
%
% See also cell2posArray, velArray, selectMeanVelocity.

% G. Teza, 2020

if isempty(str_cell)
    str_cell = {'P1','P2','P3','P4','P5','P6','P7','P8','P9','P10','P11',...
        'P12','P13','P14','P15','P16','P17','P18','P19','P20','P21','P22',...
        'P23','P24','P25','P26','P27','P28','P29','P30','P31',...
        'N1','N2','N3','N4','N5','N6','N7','N8','N9','N10',...
        'GPS_ROV 1','GPS_ROV 2',...
        'R1','R1_BIS','R2','R2BIS','R3','R4','R5'};
end

ns = numel(str_cell);   % number of stations

M = cell(ns,2);

for k = 1:ns
    sck = str_cell{k};
    fnk = [CommonPart sck '.xlsx'];
    if exist(fnk,'file')
        [ak,bk] = xlsread(fnk);
        fprintf('Data related to reflector %s managed \n',sck);
        ndk = size(ak,1);
        ndiffab = size(bk,1)-ndk;
        bkn = bk(ndiffab+1:end,:);  % bk with the same number of rows of ak
        Mk  = zeros(ndk,4);
        Ik  = zeros(ndk,3); % to manage the irregular numeric data
        for l = 1:ndk
            for m = 1:3
                a_klm = ak(l,m);
                if isnan(a_klm)
                    b_klm = bkn{l,m+1};
                    a_klm = str2double(strrep(b_klm,',','.'));
                elseif abs(a_klm) > 10^8
                    Ik(l,m) = 1;    % in this case, the data can be irregular
                end
                Mk(l,m+1) = a_klm;
            end
            % date management
            bkd = char(bkn{l,1});
            if strcmp(bkd(3),'/')
                dd = str2double(bkd(1:2));
                mm = str2double(bkd(4:5));
                yy = str2double(bkd(7:10));
                if length(bkd) > 10
                    hd = str2double(bkd(12:13));
                    md = str2double(bkd(15:16));
                else
                    hd = 0;
                    md = 0;
                end
                sd = 0;
            else    
                yy = str2double(bkd(1:4));
                mm = str2double(bkd(6:7));
                dd = str2double(bkd(9:10));
                if length(bkd) > 10
                    hd = str2double(bkd(12:13));
                    md = str2double(bkd(15:16));
                    if length(bkd) > 16
                        sd = str2double(bkd(18:19));
                    else
                        sd = 0;
                    end
                else
                    hd = 0;
                    md = 0;
                    sd = 0;
                end
            end
            dk = datenum(yy,mm,dd,hd,md,sd);
            Mk(l,1) = dk;
            if l == 1
                fprintf('Initial date %s --- ',datestr(dk));
            elseif l == ndk
                fprintf('Final date %s\n',datestr(dk));
            end
        end
        Ik = logical(Ik);
        if sum(Ik) >= 1     % in this case, at least an irregular data exists
            for n = 1:3
                if sum(Ik(:,n)) >= 1
                    Mkn = Mk(:,n+1);
                    Mknn = Mkn;         % to keep Mkn as backup  
                    Ikn = Ik(:,n);
                    Mkn1 = Mkn(Ikn);    % Irregular values
                    no1 = numel(Mkn1);  % Number of irregular values
                    cnum = (1:ndk)';
                    nnum = cnum(Ikn);   % indices of irregular values 
                    for p = 1:no1
                        p1 = max([1, nnum(p)-100]);
                        p2 = min([ndk, nnum(p)+100]);
                        Mp12 = Mkn(p1:p2);
                        Ip12 = Ikn(p1:p2);
                        refd = nanmean(abs(Mp12(~Ip12)));   % regular values in this range
                        refn = abs(Mkn1(p));
                        refn = round(log10(refn/refd));
                        Mknn(nnum(p)) = Mkn1(p)/(10^refn);
                    end
                    Mk(:,n+1) = Mknn; 
                end
            end
        end
        M(k,1) = {Mk};
    else
        fprintf('Data reflector %s unavailable \n',sck);
    end
    M(k,2) = {sck};
end